package com.wyh.controller.feemanagementcontroller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.feemanagementdto.BillingDetDTO;
import com.wyh.domain.dto.feemanagementdto.BillingitemsDTO;
import com.wyh.domain.dto.feemanagementdto.PrepaidChargeDTO;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.vo.feemanagementvo.CostSummaryVO;
import com.wyh.domain.vo.feemanagementvo.FeeFormVO;
import com.wyh.domain.vo.feemanagementvo.PaymentHistoryVO;
import com.wyh.domain.vo.feemanagementvo.ReceivablesVO;
import com.wyh.security.gfive.service.TokenService;
import com.wyh.service.feemanagementservice.BillService;
import com.wyh.service.feemanagementservice.BillsService;
import com.wyh.service.feemanagementservice.IChargingService;
import com.wyh.service.serviceImpl.feemanagementserviceimpl.BillServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 计费生成表 前端控制器
 * </p>
 *
 * @author ggt
 * @since 2024-05-22
 */
@RestController
@RequestMapping("/charging")
@RequiredArgsConstructor
@Api(tags = "收费详情")
public class ChargingController {

    private final IChargingService chargingService;

    private final BillService billService;

    private  final BillServiceImpl billServiceImpl;

    private final TokenService tokenService;

    @ApiOperation("资源的收费表单信息")
    @GetMapping("/getFeeByResourceId")
    public Result<ResponseEntity<Page<FeeFormVO>>> getFeeFormByResourceId(@RequestParam Integer resourceId, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size) {
        return Result.success(ResponseEntity.ok(chargingService.getFeeFormByResourceIdPage(resourceId,page,size)));
    }


    @ApiOperation("新增客户到虚拟资源")
    @PostMapping("/addCustomerToResource")
    public Result<ResponseEntity<Integer>> addCustomerToResource(@RequestParam Integer customId, @RequestParam Integer resourceId, @RequestParam BigDecimal fee) {
        return Result.success(ResponseEntity.ok(chargingService.addCustomerToResource(customId, resourceId, fee)));
    }


    @ApiOperation("应收详情信息")
    @GetMapping("/getReceivables")
    public Result<ResponseEntity<List<ReceivablesVO>>> getReceivableByCustomer(@RequestParam Integer customerid) {
        return Result.success(ResponseEntity.ok(billService.getBillByCustomerId(customerid)));
    }


    @ApiOperation("资源的缴费历史分页")
    @GetMapping("/getPayHistoryPage")
    public Result<ResponseEntity<Page<PaymentHistoryVO>>> getPayHistoryByResourceIdPage(@RequestParam Integer resourceid, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size) {
        return Result.success(ResponseEntity.ok(billService.getPayHistoryByResourceIdPage(resourceid,page,size)));
    }


    @ApiOperation("应收详情-开始收费")
    @GetMapping("/startCharging")
    public Result<ResponseEntity<CostSummaryVO>> getCostSummary(@RequestParam List<Integer> billdetailIds) {
        return Result.success(ResponseEntity.ok(billServiceImpl.startCharge(billdetailIds)));
    }

    @ApiOperation("应收详情-开始收费-确认收费")
    @PostMapping("/confirmCharging")
    public void confirmCharging(@RequestBody CostSummaryVO costSummaryVO, @RequestParam Integer customerId, @RequestParam Integer PaymentMethod, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Integer useid = loginUser.getUser().getId();
        billService.startChargeConfirm(costSummaryVO,customerId,PaymentMethod,useid);
    }

    @ApiOperation("预收收费")
    @GetMapping("/preCharging")
    public Result<ResponseEntity<PrepaidChargeDTO>> getPrepaidCharge(
            @RequestParam Integer resourceid,
            @RequestParam Integer customerid,
            @RequestParam Integer billingitemsid) {
        return Result.success(ResponseEntity.ok(chargingService.prepaidCharge(resourceid, customerid, billingitemsid)));
    }

    @ApiOperation("预收收费-添加费项按钮")
    @GetMapping("/getBillingitems")
    public Result<ResponseEntity<List<BillingitemsDTO>>> getBillitems(
            @RequestParam Integer resourceid,
            @RequestParam Integer customerid) {
        return Result.success(ResponseEntity.ok(chargingService.getBillingitems(resourceid, customerid)));
    }

    @ApiOperation("预收收费-生成计费按钮")
    @GetMapping("/createBilling")
    public Result<ResponseEntity<List<BillingDetDTO>>> createBilling(
            @RequestParam Integer month,
            @RequestParam Integer resourceid,
            @RequestParam BigDecimal price) {
        return Result.success(ResponseEntity.ok(chargingService.generateCharging(month, resourceid, price)));
    }























}
